package cn.tedu._05article_test.controller;

import cn.tedu._05article_test.base.response.JsonResult;
import cn.tedu._05article_test.base.response.StatusCode;
import cn.tedu._05article_test.mapper.UserMapper;
import cn.tedu._05article_test.pojo.dto.UserArticleParam;
import cn.tedu._05article_test.pojo.dto.UserLoginParam;
import cn.tedu._05article_test.pojo.vo.ArticleVO;
import cn.tedu._05article_test.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.plugins.JacksonSerializerConvention;

import javax.servlet.http.HttpSession;
@Validated
@Api(tags = "1.用户管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("login")
    @ApiOperation("用户登录功能")
    @ApiOperationSupport(order = 10)
    public JsonResult UserLogin(@RequestBody UserLoginParam userLoginParam, @ApiIgnore HttpSession session){
        log.debug("用户登录功能：");
        UserVO userVO = userMapper.selectLoginUser(userLoginParam);
        log.debug("userVO="+userVO);
        if (userVO == null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user",userVO);
        return JsonResult.ok(userVO);
    }
    @PostMapping("article")
    @ApiOperation("发表文章功能")
    @ApiOperationSupport(order = 20)
    public JsonResult addArticle(@Validated @RequestBody UserArticleParam userArticleParam,@ApiIgnore HttpSession session){
        log.debug("发表文章功能");
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        int num = userMapper.insertArticle(userArticleParam);
        log.debug("num="+num);
        if (num > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @GetMapping("selectByArticleId")
    @ApiOperation("查询文章功能")
    @ApiOperationSupport(order = 30)
    public JsonResult selectByArticle(@Range(min = 2,max = 100,message = "id必须在2~100之间") @RequestParam Long id,
                                      @ApiIgnore HttpSession session){
        log.debug("查询文章功能");
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        ArticleVO articleVO = userMapper.selectArticleId(id);
        if (articleVO == null){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        return JsonResult.ok(articleVO);
    }
}
